package com.aivin.mobileApi;

import com.aivin.entity.FollowUserBean;
import com.aivin.entity.TbPic;
import com.aivin.entity.TbUserV1;
import com.aivin.service.UserService;
import com.aivin.utils.MyStringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 *   create by aivin   20161010
 *   用户登录、注册
 */
@Controller//@Service @Component
@RequestMapping("/mobileApi")
public class UserloginAndRegController {


    @Autowired
    private UserService userService  ;



    /***
     *  http://192.168.0.119:8081/findYou/mobileApi/loginByPwd/Hnyer/123456
     *  自有账号体系。密码登录。
     * **/
    @RequestMapping(value = "/loginByPwd/{userName}/{userPwd}",
            method = {RequestMethod.POST, RequestMethod.GET},
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String  loginByPwd(@PathVariable String userName   ,@PathVariable String  userPwd  ){
       String reslut="init";

        int userID = userService.checkUserExist(userName   ) ;
        if(-1==userID){
            //用户不存在
            reslut ="error_notExsit";
        }else{
            //电话 号码  登录
            TbUserV1  bean = userService.loginByPwd(userName, userPwd);
            try {
                //登录后的用户详细情况
                if(bean==null){
                    //账号或者密码错误，登录拒绝
                    reslut ="error_reject";
                }else{

                    reslut =   JSON.toJSONString(bean) ;

                }
            } catch (JSONException e) {
                //登录意外异常
                reslut ="unknown_error";
                e.printStackTrace();
            }
        }


        return   reslut ;
    }


    /***
     *  http://192.168.0.119:8081/findYou/mobileApi/regByPwd/Hnyer/123456
     *  自有账号体系。密码注册。
     * **/
    @RequestMapping(value = "/regByPwd/{userName}/{userPwd}",
            method = { RequestMethod.GET},
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String  regByPwd(@PathVariable String userName   ,@PathVariable String  userPwd  ){
        String reslut="init";

        int userID = userService.checkUserExist(userName   ) ;
        //"-1" 表示不存在 ，可以注册。
        if( -1 ==userID){
            //账号  注册
            TbUserV1  bean = new TbUserV1();
            bean.setUuid(userName+"_"+System.currentTimeMillis());
            bean.setNickName(userName);
            //注册的用户默认添加一个头像
            bean.setUserImagURL("http://hnyer-private-data.gz.bcebos.com/pic/default_userhead.png");
            bean.setPlatform("sys");
            bean.setRegTime(MyStringUtils.getCurrentTimeStr());
            bean.setPwd(userPwd);

            //result  "-1"表示注册失败  ，大于 0 表示注册成功
            reslut  = userService.regByPwd(bean);


        }else {
            //  表示用户已经存在
            reslut="error_exsit";
        }


        return   reslut ;
    }





    /***
     *  http://192.168.0.119:8081/findYou/mobileApi/loginByThird
     *  第三方登录
     只支持post 。
     */
    @RequestMapping(value = "/loginByThird",method = {RequestMethod.POST},  produces = "text/html;charset=UTF-8" )
    @ResponseBody
    public String  loginByThird (@RequestParam("uuid") String uuid ,  @RequestParam("nickName") String nickName  ,
                                 @RequestParam("userImagURL") String userImagURL  ,@RequestParam("platform") String platform     ){

        String reslut="init";
        int userID = userService.checkUserExistByThird(uuid  ,platform) ;
        if( -1 ==userID){
            // 用户不存在，去注册一下
            TbUserV1  bean = new TbUserV1();
            bean.setUuid( uuid);
            bean.setNickName(nickName);
            //注册的用户默认添加一个头像
            bean.setUserImagURL(userImagURL);
            bean.setPlatform(platform);
            bean.setRegTime(MyStringUtils.getCurrentTimeStr());
            // QQ+微博+微信 登录注册的 是不需要密码的 .根据时间戳生成一个密码
            bean.setPwd(nickName+"_"+platform+"_"+System.currentTimeMillis());

            //result  "-1"表示注册失败  ，大于 0 表示注册成功
            reslut  = userService.regByPwd(bean);


        } else {
            //存在的话就直接返回
            reslut = String.valueOf(userID) ;
        }



        return   reslut ;
    }






}
